<?php

require_once 'ActionFramework/start.php';

class ManageRejectionAction extends Action {
	
	const INPUT_REJECTED_USER_ID = "reject_user_id";
	const INPUT_OPERATION = "operation";
	
	const REJECT = 'reject';
	const UNREJECT = 'unreject';
	
	public function __construct() {
		parent::__construct();
		$this->add_input(new Input(ManageRejectionAction::INPUT_REJECTED_USER_ID));
		$this->add_input(new MultiChoiceInput(
			ManageRejectionAction::INPUT_OPERATION, 
			null, 
			array(
				ManageRejectionAction::REJECT => ManageRejectionAction::REJECT, 
				ManageRejectionAction::UNREJECT => ManageRejectionAction::UNREJECT
			)
		));
	}
	
	public function trigger($request) {
		$user = $_SESSION['user'];

		$operation = $request[ManageRejectionAction::INPUT_OPERATION];
		$rejected_user_id = intval($request[ManageRejectionAction::INPUT_REJECTED_USER_ID]);
		
		global $DB;
		if ($operation == ManageRejectionAction::REJECT) {
			$user = $DB->reject($user, $rejected_user_id);
			$message = 'The user is now blocked and can no longer see or contact you.';
		}
		else {
			$user = $DB->unreject($user, $rejected_user_id);
			$message = 'The user is no longer blocked and can again see and contact you.';
		}
		
		$view = r(new ViewMatchingAction())->trigger($request);
		$view->add_message($message);
		return $view;
		
	}
	
}

?>
